package de.ars.client.items;

import java.util.Enumeration;
import java.util.Vector;


import de.ars.client.datahandling.IPersonListener;
import de.ars.client.datahandling.InformationGroup;
import de.ars.client.datahandling.InformationItem;
import de.ars.client.datahandling.Person;
import de.ars.client.datahandling.PropertyReader;
import de.ars.client.items.animators.AddAttributeItemAnimator;
import de.ars.client.items.elements.AbstractItem;
import de.ars.client.items.elements.ChooseItem;
import de.ars.client.items.elements.ListItem;
import de.ars.client.items.elements.StarItem;
import de.ars.client.paintSystems.StringPainter;
import de.ars.client.screens.MainScreen;

public class AddAttributeItem extends AbstractSlideItem
{	
	String filename;
	
	public AddAttributeItem(int _width, int _height, MainScreen _parent, StringPainter _stringPainter)
	{
		super(_width,_height,_parent,_stringPainter);
		
		this.animator = new AddAttributeItemAnimator(this);
        this.animator.createNewAnimationPoint(this.layout.getAddPersonOptionClosePos());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonOptionCloseSize());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonOptionCloseTextPos());
        this.animator.setLayout(this.layout);
        
        this.entryItems = new Vector();        
        InformationItem item;
        InformationGroup group = (InformationGroup)this.parent.getInformationGroup().get("person");
        Enumeration e = group.getEnumeration();
        while(e.hasMoreElements())
        {
        	item = (InformationItem)e.nextElement();
        	String key = "person." + item.GetKey();        	
        	if (item.GetTyp().equals(InformationItem.TYP_CHOOSE))
            {
	            this.entryItems.addElement( new ChooseItem( this.stringPainter, PropertyReader.getProperty( "label." + key ) + ": ", key,item.getOptions() ) );
            }
            else if (item.GetTyp().equals(InformationItem.TYP_TEXT))
            {
            	this.entryItems.addElement( new ListItem( this.stringPainter, PropertyReader.getProperty( "label." + key ) + ": ", "", key ) );
            }
            else if (item.GetTyp().equals(InformationItem.TYP_DIGIT))
            {
            	this.entryItems.addElement( new ListItem( this.stringPainter, PropertyReader.getProperty( "label." + key ) + ": ", "", key ) );
            }
            else if (item.GetTyp().equals(InformationItem.TYP_STARS))
            {
            	StarItem tempStarItem = new StarItem( PropertyReader.getProperty( "label." + key ) + ": ","0" ,true);
            	tempStarItem.setKey(key);
            	this.entryItems.addElement( tempStarItem );	
            }
            else if (item.GetTyp().equals(InformationItem.TYP_DATE))
            {
            	
            }
        }
	}
	
	public String getFilename()
    {
    	return this.filename;
    }
	
	private void SaveData()
	{
		Enumeration e = this.entryItems.elements();
		AbstractItem currentItem = null;
		while(e.hasMoreElements())
		{
			currentItem = (AbstractItem)e.nextElement();
			if(currentItem.getValue()!= null && !"".equals(currentItem.getValue()))
			{
				this.parent.getSpecificPerson().putInformation(currentItem.getKey(), currentItem.getValue());
			}
		}
		this.parent.newPersonAction(IPersonListener.ACTION_NEWPERSONOTPION, null);
	}
	
	public boolean keyPressed(int _key)
	{
		if (isOpen())
		{
			AbstractItem tempItem = ((AbstractItem)this.entryItems.elementAt(this.selectedItemIndex));
			switch(_key)
	    	{
		    	case -1:
					if(this.selectedItemIndex > 0)
					{
						this.animationType = ANIMATION_SCROLL_UP;
		                ((AddAttributeItemAnimator)this.animator).animateScrollUp(tempItem.getHeight());
		                this.selectedItemIndex--;
					}				
					return true;
				case -2:
					if(this.selectedItemIndex < this.entryItems.size()-1)
					{
						this.animationType = ANIMATION_SCROLL_DOWN;
		                ((AddAttributeItemAnimator)this.animator).animateScrollDown(tempItem.getHeight());
		                this.selectedItemIndex++;
					}
					return true;
	    		case -6:
	    			this.animationType = ANIMATION_CLOSE;
	    			this.animator.closeItem();
	    			this.state = STATE_CLOSE_ANIMATION;
	    			return true;
	    		case -7:
	    			//speichern und fenster schliessen
	    			this.SaveData();
	    			this.animationType = ANIMATION_CLOSE;
	    			this.animator.closeItem();
	    			this.state = STATE_CLOSE_ANIMATION;
	    			return true;
	    		default:
	    			tempItem.keyPressed(_key);
	    			return true;
	    	}
		}
		return false;
	}
	
	public void open()
	{
		super.open();
		clear();
	}
	
	private void clear()
	{
		Enumeration e = this.entryItems.elements();
		while(e.hasMoreElements())
			((AbstractItem)e.nextElement()).setValue("");
	}
}